///////////////////////////////////////////////////////////////////////
// Project: XNA Quake3 Lib - BSP
// Author: Craig Sniffen
// Copyright (c) 2006-2009 All rights reserved
///////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAQ3Lib.Q3BSP
{
    enum Q3BSPBlendFuncFactor
    {
        Zero,
        One,
        Source,
        InverseSource,
        SourceAlpha,
        InverseSourceAlpha,
        Destination,
        InverseDestination,
        DestinationAlpha,
        InverseDestinationAlpha
    }

    public class Q3BSPMaterialStage
    {
        public Texture2D Texture;

        public string TextureFilename;
        public Vector2 Scale;
        public Vector2 Scroll;
        public float Rotation;

        public bool IsLightmapStage;
        
        Vector2 accumulatedScroll;
        float accumulatedRotation;

        public string stageName;
        public string textureName;
        public string tcModName;
        public string samplerName;
        public string inputName;

        public bool IsSpecialCaseStage
        {
            get { return this.IsLightmapStage; }
        }

        public Q3BSPMaterialStage()
        {
            this.TextureFilename = "";
            this.Scroll = Vector2.Zero;
            this.Scale = Vector2.One;
            this.Rotation = 0;

            this.IsLightmapStage = false;
        }

        public void InitializeEffectNames(int stageNumber)
        {
            this.stageName = "stage" + stageNumber;
            this.textureName = stageName + "_" + TextureFilename.Substring(TextureFilename.LastIndexOf('/') + 1);
            this.tcModName = stageName + "_tcmod";
            this.samplerName = stageName + "_sampler";
            this.inputName = stageName + "_input";
        }

        public float[] MakeTcModMatrix(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            accumulatedScroll += Scroll * elapsed;
            accumulatedRotation += Rotation * elapsed;

            Matrix m = Matrix.CreateTranslation(-.5f, -.5f, 0);
            m *= Matrix.CreateRotationZ(accumulatedRotation);
            m *= Matrix.CreateTranslation(.5f, .5f, 0);
            m *= Matrix.CreateScale(Scale.X, Scale.Y, 0);
            m *= Matrix.CreateTranslation(accumulatedScroll.X, accumulatedScroll.Y, 0);

            float[] threeByTwo = new float[6];
            threeByTwo[0] = m.M11;
            threeByTwo[1] = m.M12;

            threeByTwo[2] = m.M21;
            threeByTwo[3] = m.M22;

            threeByTwo[4] = m.Translation.X;
            threeByTwo[5] = m.Translation.Y;

            return threeByTwo;
        }

        public override string ToString()
        {
            return "Tex: " + TextureFilename;
        }

        internal void SetEffectParameters(ref Effect effect, GameTime gameTime)
        {
            effect.Parameters[this.textureName].SetValue(this.Texture);
            effect.Parameters[this.tcModName].SetValue(this.MakeTcModMatrix(gameTime));
        }
    }
}
